const express = require('express')
const router = express.Router()
const {logI} = require('../log.js')
const {retError, retSuccess} = require('../utils/ret')
const userDb = require('../db/user-db')
const checkType = require('../utils/checkType')
const checkToken = require('../utils/checkToken')
const checkDeviceNo = require('../utils/checkDeviceNo')
const moment = require('moment')

router
    .post('/', async(req, res) => {
        let {body: {maddr,token,buildingUUID,meshUUID,deviceNo}} = req
        logI('A-S', maddr, 'delete_building_mesh', `${JSON.stringify(req.body)}`, 'null');
        try{
            const error = checkType.getBuilding(req.body)
            if(error){
                retError("delete_building_mesh",maddr, 10001,  error.details[0].message, res);
            }else{
                let msg = {};
                msg = await checkToken(token,maddr);
                if(msg.code === 0){
                    const deviceInfo={
                        'maddr':maddr,
                        'deviceNo':deviceNo
                    }
                    let device={};
                    device = await checkDeviceNo(deviceInfo);
                    if(device.code === 0){
                        let result = {};
                        if(buildingUUID){
                            result = await userDb.selectRole(maddr,buildingUUID);
                            if(result.role === 0){
                                await userDb.updateBuildingAll(buildingUUID);
                                await userDb.updateMeshDel(maddr,buildingUUID);
                            }else{
                                await userDb.updateBuildingMaddrDel(maddr,buildingUUID);
                                await userDb.updateMeshDel(maddr,buildingUUID);
                                await userDb.delShare(moment().format("YYYY-MM-DD HH:mm:ss"),maddr,buildingUUID);
                            }
                            retSuccess("delete_building_mesh", maddr,0, "", res);
                        }else if(meshUUID){
                            result = await userDb.selectRoleMesh(maddr,meshUUID);
                            if(result && result.role === 0){
                                await userDb.updateMesh(maddr,meshUUID);
                                await userDb.updateBuildingVersionByMesh(maddr,meshUUID);
                                retSuccess("delete_building_mesh", maddr,0, "", res);
                            }else{
                                retError("delete_building_mesh", maddr,10019, "mesh does not exist ", res);
                            }
                        }else{
                            retError("delete_building_mesh", maddr,10001, "lose key", res);
                        }
                    }else{
                        retError("delete_building_mesh", maddr,device.code, device.body, res);
                    }
                }else{
                    retError("delete_building_mesh", maddr,msg.code, msg.body, res);
                }
            }
        } catch (e) {
            retError("delete_building_mesh", maddr,10001, e, res);
        }

    })
module.exports = router
